package com.tb.service;

import com.tb.mapper.ACommentMapper;
import com.tb.model.AComment;
import com.tb.model.ACommentExample;
import com.tb.util.MyConstant;
import com.tb.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: wangguichun
 * @create: 2018-04-14 21:47
 **/

@Service
public class ACommentService {
    @Autowired
    private ACommentMapper aCommentMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private SensitiveWordService sensitiveWordService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private IllegalityService illegalityService;

    public List<AComment> getCommentByAnswerId(Integer answerId) {
        ACommentExample example = new ACommentExample();
        ACommentExample.Criteria criteria = example.createCriteria();
        criteria.andAnswerIdEqualTo(answerId);
        List<AComment> aCommentList = aCommentMapper.selectByExample(example);
//        为评论获取用户
        for (AComment aComment : aCommentList) {
            aComment.setUser(userService.getUserById(aComment.getUserId()));
        }
        return aCommentList;
    }

    public void addComment(AComment aComment) {
        String content = aComment.getContent();
        aComment.setContent(MyUtils.purifyString(content,sensitiveWordService.getStringList()));
        aCommentMapper.insertSelective(aComment);
    }

    public AComment getAnswerCommentById(Integer objectId) {
        return aCommentMapper.selectByPrimaryKey(objectId);
    }

    public void deleteAnswerCommentById(int id) {
        //删除之前连带删除消息表和违规记录表中的记录
        messageService.removeRecordByTypeAndObjectId(MyConstant.ACT_COMMENT_ANS,id);
        illegalityService.removeRecordByTypeAndObjectId(MyConstant.ACT_COMMENT_ANS,id);
        //真正删除问题记录
        aCommentMapper.deleteByPrimaryKey(id);
    }

}
